package com.acane.instagram.controller;

import com.acane.instagram.pojo.Result;
import com.acane.instagram.pojo.dto.CommentDTO;
import com.acane.instagram.service.CommentService;
import com.acane.instagram.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.UUID;

@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    @Autowired
    private PostService postService;

    @GetMapping("/get/all")
    public Result getAllComment(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String commenterUserName,
            @RequestParam(required = false) String content,
            @RequestParam(required = false)
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,  // 改为LocalDate
            @RequestParam(required = false)
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate
    ) {
        return commentService.getAllComment(
                page,
                size,
                commenterUserName,
                content,
                startDate,
                endDate
        );
    }

    @GetMapping("/get/id")
    public Result getCommentById(@RequestParam String commentId) {
        CommentDTO commentDTO = commentService.getById(commentId);
        if (commentDTO != null) {
            return Result.success(commentDTO);
        }
        return Result.failure(400, "评论不存在");
    }

    @GetMapping("/history")
    public Result getCommentHistory(@RequestParam String userId) {
        return commentService.getCommentHistory(userId);
    }

    @GetMapping("/list")
    public Result getCommentList(@RequestParam String postId) {
        return commentService.getCommentList(postId);
    }

    @PostMapping("/add")
    public Result addComment(@RequestBody CommentDTO commentDTO) {
        commentDTO.setId(UUID.randomUUID().toString().replace("-", ""));
        commentDTO.setTimestamp(Timestamp.valueOf(LocalDateTime.now()).toString());
        boolean result = commentService.save(commentDTO);
        // 更新帖子评论数
        int i = postService.addCommentCount(commentDTO.getPostId());

        if (result && i > 0) {
            return Result.success(commentDTO);
        }
       return Result.failure(400, "添加失败");
    }

    @PostMapping("/like")
    public Result likePost(@RequestParam String commentId) {
        return Result.success(commentService.likeComment(commentId));
    }

    @PostMapping("/unlike")
    public Result unlikePost(@RequestParam String commentId) {
        return Result.success(commentService.unlikeComment(commentId));
    }

    @GetMapping("/get/one")
    public Result getOneComment(@RequestParam String commentId) {
        System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++接收到的commentId：" + commentId);
        CommentDTO commentDTO = commentService.getById(commentId);
        if (commentDTO != null) {
            return Result.success(commentDTO);
        }
        return Result.failure(400, "评论不存在");
    }

    @GetMapping("/like/history")
    public Result getCommentLikeHistory(@RequestParam String userId) {
        return commentService.getCommentLikeHistory(userId);
    }

    @GetMapping("/isLike")
    public Result judgeIfAlreadyLike(@RequestParam String commentId, @RequestParam String likerId) {
        return Result.success(commentService.judgeIfAlreadyLike(commentId, likerId));
    }

    @GetMapping("/count/get")
    public Result getCommentCount(@RequestParam String postId) {
        return Result.success(commentService.countByPostId(postId));
    }
}
